<template>
  <BaseMainSlot :show-search-box="false">
    <template v-slot:btn-box>
      <ActionBar :add-active="addActive" :del-active="delActive" @add="add" @del="del" />
    </template>
    <template v-slot:dialog>
      <el-dialog
        :title="dialogTitle + '物联单位信息'"
        :visible.sync="dialogFormVisible"
        :before-close="cancel"
        width="40%"
      >
        <el-form
          ref="formData"
          label-position="left"
          :model="formData"
          class="el-form"
        >
          <el-form-item label="单位名称:" prop="unitName" class="w100p">
            <el-input
              v-model="formData.unitName"
              :disabled="!canInput"
              clearable
            />
          </el-form-item>
          <el-form-item label="单位符号:" prop="symbol" class="w100p">
            <el-input
              v-model="formData.symbol"
              :disabled="!canInput"
              clearable
            />
          </el-form-item>
          <el-form-item label="描述:" prop="description" class="w100p">
            <el-input
              v-model="formData.description"
              :disabled="!canInput"
              clearable
            />
          </el-form-item>
        </el-form>
        <div v-if="nowChanceStau !== ''" slot="footer" class="dialog-footer">
          <el-button class="zh-button-one" @click="cancel('formData')">
            取 消
          </el-button>
          <el-button class="zh-button-two" type="primary" @click="save('formData')">
            保 存
          </el-button>
        </div>
      </el-dialog>
    </template>
    <template v-slot:table-box>
      <div class="zhtabletitle">所有物联单位</div>
      <el-table
        v-loading="loading"
        :data="tableData"
        stripe
        border
        height="calc(100% - 75px)"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column type="index" label="序号" width="100" />
        <el-table-column prop="unitName" label="物联单位名称" />
        <el-table-column prop="symbol" label="单元符号" />
        <el-table-column prop="description" label="描述" />
        <el-table-column width="120" label="操作">
          <template v-if="scope.row.iotUnitId" slot-scope="scope">
            <el-button class="text-btn-cursor" type="text" size="small" @click="handleEdit(scope.row)"><i class="el-icon-edit-outline"></i> 修改</el-button>
            <el-button class="text-btn-cursor cursor-delete" type="text" size="small" @click="handleDelete(scope.row)"><i class="el-icon-delete"></i> 删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        :page-size="pageSize"
        :current-page="currentPage"
        :total-size="totalSize"
        @changePageSize="changePageSize"
        @changeCurrentPage="changeCurrentPage"
      />
    </template>
  </BaseMainSlot>
</template>
<script>
import ActionBar from "@/components/ActionBar";
import Pagination from "@/components/Pagination";
import nowpagereq from "@/api/basic-info-manage/iot-unit-management";
import BaseMainSlot from "@/components/BaseMainSlot/newBaseMainSlot"
import { mymixin } from "@/utils/mixin"
// import { getToken } from '@/utils/auth';
export default {
  name: "IotUnitManagement",
  components: { ActionBar, Pagination, BaseMainSlot },
  mixins: [mymixin],
  data() {
    return {
      // 是否能输入
      // -------
      canInput: false,
      rules: {},
      // 选中的数据项数组 employeeId组成 (此数组最多为一个)
      nowselectedList: [],
      dialogTitle: "",
      dialogFormVisible: false
    };
  },
  watch: {},
  created() {
    this.getTableDataList()
  },
  mounted() {},
  methods: {
    // 获取所有农业类型表格
    getTableDataList() {
      this.loading = true;
      let params = {
        page: this.currentPage,
        size: this.pageSize
      }
      nowpagereq.getbaseUnitList(params).then(res => {
        this.loading = false;
        if (res.code === 0) {
          this.$message.error('所有物联单位信息表格获取失败！')
        }
        let data = res.data;
        // 赋值总条数
        this.totalSize = data.totalElements;
        // 赋值数组
        this.tableData = data.content;
        if (this.tableData.length < this.pageSize) {
          let havline = this.tableData.length % this.pageSize
          if (havline < this.pageSize) {
            const remain = this.pageSize - havline;
            for (var i = 0; i < remain; i++) {
              this.tableData.push({ 'index': i + this.tableData.length - 1 });
            }
          }
        }
        this.resetActionBar();
      });
    },
    search() {
      this.currentPage = 1;
      this.getTableDataList();
    },
    changePageSize(pageSize) {
      this.pageSize = pageSize;
      this.getTableDataList();
    },
    changeCurrentPage(currentPage) {
      this.currentPage = currentPage;
      this.getTableDataList();
    },
    handleEdit(row) {
      if (row.iotUnitId) {
        this.openDialog('edit');
        this.formData = row;
      }
    },
    // 添加
    add() {
      this.openDialog('add');
      this.formData = {};
    },
    // 取消
    cancel() {
      this.closeDialog();
      this.formData = {}
    },
    handleSelectionChange(list) {
      this.nowselectedList = [];
      // let id = getToken('companyId')
      this.nowselectedList = list.map(item => {
        return item.iotUnitId
      });
      if (this.nowselectedList && this.nowselectedList.length > 0) this.delActive = true;
      else this.delActive = false;
    },
    delmessage() {
      if (this.nowChanceStau === 'del') {
        nowpagereq.delUnitlist(this.nowselectedList).then(
          res => {
            if (res.code === 1) {
              this.getTableDataList()
              this.$message.success('删除成功')
            } else {
              this.$message.error('删除失败')
            }
          }
        )
      }
    },
    handleDelete(data) {
      nowpagereq.delUnitlist([data.iotUnitId]).then(res => {
        if (res.code === 1) {
          this.$message.success('删除成功')
          this.getTableDataList();
        } else {
          this.$message.error('删除失败')
        }
      })
    },
    save() {
      if (this.nowChanceStau === 'add') {
        nowpagereq.addUnitlist(this.formData).then(
          res => {
            if (res.code === 1) {
              this.getTableDataList()
              this.closeDialog();
              this.$message.success('添加成功')
            } else {
              this.$message.error('添加失败')
            }
          }
        )
      } else if (this.nowChanceStau === 'edit') {
        nowpagereq.editUnitlist(this.formData).then(
          res => {
            if (res.code === 1) {
              this.getTableDataList()
              this.closeDialog();
              this.$message.success('修改成功')
            } else {
              this.$message.error('修改失败')
            }
          }
        )
      }
    },
    openDialog(type) {
      this.dialogFormVisible = true;
      this.dialogTitle = type === 'add' ? '新增' : '编辑'
      this.nowChanceStau = type;
    },
    closeDialog() {
      this.dialogFormVisible = false;
      this.dialogTitle = null;
      this.nowChanceStau = '';
    }
  }
};
</script>
<style lang="scss" scoped>
.zhtabletitle{
  color:#31f7dd;
  margin-bottom: 10px;
}
</style>
